/**
 * 在地图上获取经纬度
 */

function MapCoords(container, settings){
	var defaultOptions = {
			path: '',
			type:null,
			parkid:null,
			view: false,
			size: 200,
			center: "河北",//{lng:lng, lat:lat, zoom:zoom}或字符串格式的位置名称
			marker: null,//{lng:lng, lat:lat, type:type, id:id, name:name}
			boundary: null,//边界名（省、市或县的名称）
			onMapStatus: null,//地图状态
			onMarkerDragend: null//参数：中心坐标{lng:lng, lat:lat}
		};
	var g = this, p = jQuery.extend({}, defaultOptions, settings);;
	g._marks = new JSHashtable();
	g._datas = new JSHashtable();
	
	g._init = function(){
		g.view = (p.view===true || p.view==='true');
		g.map = new BMap.Map(container);
		if(typeof p.center == "object"){
			g.map.centerAndZoom(new BMap.Point(p.center.lng, p.center.lat), p.center.zoom);
		}else{
			g.map.centerAndZoom(p.center);
		}
		
		g._initMove = 0;
		
		g.map.enableScrollWheelZoom();
		g.map.enableContinuousZoom();
		g.map.addControl(new BMap.NavigationControl({type:BMAP_NAVIGATION_CONTROL_SMALL}));
		g.map.addControl(new BMap.ScaleControl({anchor:BMAP_ANCHOR_BOTTOM_RIGHT}));
		g.map.addControl(new BMap.MapTypeControl({
			type:BMAP_MAPTYPE_CONTROL_HORIZONTAL, mapTypes:[BMAP_NORMAL_MAP, BMAP_SATELLITE_MAP, BMAP_HYBRID_MAP]
		}));
		
		//缩放事件
		g.map.addEventListener("zoomend", function(evn){
			g._load();
		});
		//平移事件
		g.map.addEventListener("moveend", function(){
			g._load();
		});
		
		var center = !p.marker ? g.map.getCenter() : p.marker;
		var opt = {}, icon = g._getMarkerIcon(p.type);
		if(icon){
			opt.icon = icon;
		}
		g.curMarker = new BMap.Marker(new BMap.Point(center.lng, center.lat), opt);
		g.map.addOverlay(g.curMarker);
		if(!g.view){
			g.curMarker.enableDragging();
			if(p.onMarkerDragend){
				g.curMarker.addEventListener("dragend", function(evt){
					p.onMarkerDragend.call(g, {lng: evt.point.lng, lat: evt.point.lat});
				});
			}
		}
		if(p.onMarkerDragend){
			p.onMarkerDragend.call(g, center);
		}
		
		//绘制边界
		if(p.boundary){
			g.bmapBoundary = new BMap.Boundary();
			g.bmapBoundary.get(p.boundary, function(rs){
				if(rs){
					var count = rs.boundaries.length;
					for(var i=0; i<count; i++){
			            var polygon = new BMap.Polygon(rs.boundaries[i], {strokeWeight:2, strokeColor:'#ff0000', fillColor:""});
			            g.map.addOverlay(polygon);
			        }
				}
		    });
		}
		
		g._load();
	};
	
	g._mapStatus = function(message){
		if(p.onMapStatus){
			var status = {center: g.map.getCenter(), zoom: g.map.getZoom()},
				bounds = g.map.getBounds(),
				southWest = bounds.getSouthWest(),
				northEast = bounds.getNorthEast();
			if(southWest && northEast){
				status.bounds = {E: northEast.lng, S: southWest.lat, W: southWest.lng, N: northEast.lat};
			}
			if(message){
				status.message = message;
			}
			p.onMapStatus.call(g, status);
		}
	};
	
	//获取标注坐标
	g.getMarkerCoords = function(){
		if(g.curMarker){
			var pos = g.curMarker.getPosition();
			return {lng:pos.lng, lat:pos.lat};
		}
		return null;
	};
	
	//设置标注坐标
	g.setMarkerCoords = function(lng, lat){
		if(!g.curMarker){
			return;
		}
		g.curMarker.setPosition(new BMap.Point(lng, lat));
		if(p.onMarkerDragend){
			p.onMarkerDragend.call(g, {lng: lng, lat: lat});
		}
	};
	
	//获取标注图标
	g._getMarkerIcon = function(type, mini){
		return null;
	};
	
	g._load = function(){
		var center = g.map.getCenter();
		g._mapStatus();
		g._initMove++;
		if(g._initMove < 3){
			g.setMarkerCoords(center.lng, center.lat);
		}
	};
	
	g._init();
}




